#include "AnimationHandler.h"

AnimationHandler::AnimationHandler(sf::Texture* texture) : _texture(texture)
{
	sf::IntRect rect(0,40,40,40);

	_textureRect[Left1]=rect;
	rect.left=40;
	_textureRect[Left2]=rect;
	rect.left=80;
	_textureRect[Left3]=rect;
	rect.left=120;
	_textureRect[Left4]=rect;

	rect.left=0;
	rect.top=80;
	_textureRect[Right1]=rect;
	rect.left=40;
	_textureRect[Right2]=rect;
	rect.left=80;
	_textureRect[Right3]=rect;
	rect.left=120;
	_textureRect[Right4]=rect;

	//_shape.setTexture(_texture);
	//_shape.setTextureRect(_textureRect[_currentDirection]);

}
AnimationHandler::~AnimationHandler()
{
	delete _texture;
}

void AnimationHandler::attachTexture(Movable* movable)
{
	movable->getShape()->setTexture(_texture);
	movable->getShape()->setTextureRect(_textureRect[movable->_currentDirection]);
}

void AnimationHandler::updateMovable(Movable* movable)
{
	if ( movable->getVelocity().y!=0)
	{
		if(movable->getVelocity().x<0)
			movable->_currentDirection=Left2;
		else
			movable->_currentDirection=Right2;
	}
	else if(movable->getVelocity().x<0)
	{
		switch (movable->_currentDirection)
		{
		case Left1: movable->_currentDirection=Left2; break;
		case Left2: movable->_currentDirection=Left3; break;
		case Left3: movable->_currentDirection=Left4; break;
		case Left4: movable->_currentDirection=Left1; break; 
		default: movable->_currentDirection=Left1; break;
		}
	}
	else if(movable->getVelocity().x>0)
	{
		switch (movable->_currentDirection)
		{
		case Right1: movable->_currentDirection=Right2; break;
		case Right2: movable->_currentDirection=Right3; break;
		case Right3: movable->_currentDirection=Right4; break;
		case Right4: movable->_currentDirection=Right1; break;
		default: movable->_currentDirection=Right1; break;
		}
	}
	movable->getShape()->setTextureRect(_textureRect[movable->_currentDirection]);
}